package com.tk.exam.dao;

import com.github.pagehelper.PageInfo;
import com.tk.exam.domain.*;
import com.tk.exam.mapper.TkExamPaperExtMapper;
import com.tk.exam.mapper.TkExamPaperMapper;
import com.tk.exam.vo.paper.ExamPaperVo;
import com.tk.exam.mapper.TkRPaperQuestionMapper;
import com.tk.exam.service.impl.TkExamPaperServiceImpl;
import com.tk.exam.vo.paper.GetStuJudgeQAResp;
import com.tk.exam.vo.paper.GetStuQAReq;
import com.tk.exam.vo.paper.SearchPaperListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Slf4j
public class TkExamPaperDao {
    @Autowired
    private TkExamPaperExtMapper tkExamPaperExtMapper;
    @Autowired
    private TkExamPaperMapper tkExamPaperMapper;
    @Autowired
    private TkRPaperQuestionMapper tkRPaperQuestionMapper;
    public PageInfo<SearchPaperListResp> searchPaperListByPageWithPage(){
        return new PageInfo<SearchPaperListResp>(tkExamPaperExtMapper.searchPaperListByPage());
    }
    public PageInfo<ExamPaperVo> searchExamPapersWithPage(){
        return new PageInfo<ExamPaperVo>(tkExamPaperMapper.searchExamPapers());
    }


    public PageInfo<TkRPaperQuestion> getStuJudgeQAWithPage(GetStuQAReq req, byte quesType){
        TkRPaperQuestionExample example=new TkRPaperQuestionExample();
        example.or().andExamPaperIdEqualTo(req.getExamPaperId()).andQuestionTypeEqualTo(quesType);
        return new PageInfo<TkRPaperQuestion>(tkRPaperQuestionMapper.selectByExample(example));
//        return new PageInfo<>(tkRPaperQuestionMapper.getStuJudgeQAWithPage(req.getExamPaperId(),quesType,req.getStuId(),req.getExamId()));
    }

    @Transactional
    public TkExamPaper save(TkExamPaper tkExamPaper) {
        Integer id;
        if (tkExamPaper.getId() != null) {
            id = this.tkExamPaperMapper.updateByPrimaryKey(tkExamPaper);
        } else {
            id = this.tkExamPaperMapper.insert(tkExamPaper);
            log.info(id.toString());
            log.info(tkExamPaper.getId().toString());
        }
        return this.tkExamPaperMapper.selectByPrimaryKey(tkExamPaper.getId());
    }

    public TkExamPaper findById(Integer id){
        return this.tkExamPaperMapper.selectByPrimaryKey(id);
    }
}
